package org.cuit.epoch.seon.top;

import java.util.Optional;

// 文件管理系统
class FileSystemManager {
    private Directory root;
    private Directory currentDirectory;
    private FileSystemComponent clipboard;

    public FileSystemManager() {
        root = new Directory("Root");
        currentDirectory = root;
    }

    public void showCurrentDirectory() {
        currentDirectory.showDetails("");
    }

    public void createFile(String name, int size) {
        currentDirectory.addComponent(new File(name, size));
    }

    public void createDirectory(String name) {
        currentDirectory.addComponent(new Directory(name));
    }

    public void delete(String name) {
        Optional<FileSystemComponent> component = currentDirectory.findComponent(name);
        component.ifPresent(FileSystemComponent::delete);
    }

    public void rename(String oldName, String newName) {
        Optional<FileSystemComponent> component = currentDirectory.findComponent(oldName);
        component.ifPresent(c -> c.rename(newName));
    }

    public void copy(String name) {
        Optional<FileSystemComponent> component = currentDirectory.findComponent(name);
        if (component.isPresent()) {
            clipboard = component.get();
            System.out.println("Copied: " + clipboard.getName());
        } else {
            System.out.println("Component not found.");
        }
    }

    public void paste() {
        if (clipboard != null) {
            if (clipboard instanceof File) {
                File file = (File) clipboard;
                currentDirectory.addComponent(new File(file.getName(), file.getSize()));
                System.out.println("Pasted file: " + file.getName());
            } else if (clipboard instanceof Directory) {
                Directory dir = (Directory) clipboard;
                currentDirectory.addComponent(new Directory(dir.getName()));
                System.out.println("Pasted directory: " + dir.getName());
            }
        } else {
            System.out.println("Clipboard is empty.");
        }
    }

    public void changeDirectory(String name) {
        Optional<FileSystemComponent> component = currentDirectory.findComponent(name);
        if (component.isPresent() && component.get() instanceof Directory) {
            currentDirectory = (Directory) component.get();
        } else {
            System.out.println("Invalid directory.");
        }
    }

    public void showHelp() {
        System.out.println("Commands:");
        System.out.println("  ls <dir>           - List directory contents");
        System.out.println("  mkfile <name> <size> - Create a file");
        System.out.println("  mkdir <name>      - Create a directory");
        System.out.println("  rm <name>         - Delete a file or directory");
        System.out.println("  rename <old> <new> - Rename a file or directory");
        System.out.println("  copy <name>       - Copy a file or directory");
        System.out.println("  paste             - Paste from clipboard");
        System.out.println("  cd <name>         - Change directory");
        System.out.println("  exit              - Exit the program");
    }
}